package com.sduept.chargeRec;

import org.ssssssss.magicapi.Const;
import org.ssssssss.magicapi.core.service.MagicAPIService;
import org.ssssssss.magicapi.core.service.impl.DefaultMagicAPIService;
import org.tio.client.intf.TioClientHandler;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;

public class CdzlbClientHandler implements TioClientHandler {
    private static CdzlbPacket heartbeatPacket = new CdzlbPacket();
    public  String url;
    /**
     * 解码：把接收到的ByteBuffer，解码成应用可以识别的业务消息包
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    4个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */

    @Override
    public CdzlbPacket decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws TioDecodeException {
        Map<String,Object> params = new HashMap<>();
        params.put("buffer",buffer);
        return Const.getService().execute("POST","/network/socketClientDecode", params);
    }
    /**
     * 编码：把业务消息包编码为可以发送的ByteBuffer
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    4个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */
    @Override
    public ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
        System.out.println("开始编码");
        //创建一个新的bytebuffer
        ByteBuffer buffer = ByteBuffer.allocate(((CdzlbPacket)packet).getPacketLength());
        //设置字节序
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        //写入消息体，消息的长度等于60+data的长度
        buffer.put(((CdzlbPacket)packet).getMacAddress());
        buffer.put(((CdzlbPacket)packet).getOriginMac());
        buffer.putInt(((CdzlbPacket)packet).getPacketLength());
        buffer.putShort(((CdzlbPacket)packet).getPacketType());
        buffer.putShort(((CdzlbPacket)packet).getCode());
        buffer.put(((CdzlbPacket)packet).getMessage());
        buffer.put(((CdzlbPacket)packet).getSpare());
        buffer.putShort(((CdzlbPacket)packet).getComCounter());
        buffer.putShort(((CdzlbPacket)packet).getPacketNumber());
        buffer.putShort(((CdzlbPacket)packet).getPacketCounter());
        buffer.putShort(((CdzlbPacket)packet).getPacketResend());
        buffer.putShort(((CdzlbPacket)packet).getComResend());
        buffer.putInt(((CdzlbPacket)packet).getDataLength());
        buffer.putShort(((CdzlbPacket)packet).getPointLogo());
        buffer.put(((CdzlbPacket)packet).getSpare2());

        //写入数据
        if (((CdzlbPacket)packet).getData() != null) {
            buffer.put(((CdzlbPacket)packet).getData());
        }
        return buffer;
    }
    /**
     * 处理消息
     */
    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
//        Map<String,Object> params = new HashMap<>();
//        params.put("packet",(CdzlbPacket) packet);
//        Const.getService().execute("POST","/network/socketClientHandler", params);
        System.out.println("clienthandle");
    }
    /**
     * 此方法如果返回null，框架层面则不会发心跳；如果返回非null，框架层面会定时发本方法返回的消息包
     */


    @Override
    public CdzlbPacket heartbeatPacket(ChannelContext channelContext) {
        return null;
    }
}

